package com.qf.zyh.web.controller;

import com.qf.zyh.common.Const;
import com.qf.zyh.common.ResponseResult;
import com.qf.zyh.common.ResultType;
import com.qf.zyh.entity.AdminUser;
import com.qf.zyh.services.AdminUserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/user")
public class AdminUserController {
    @Autowired
    private AdminUserServices aus;
    @RequestMapping("/login")
    public ResponseResult login(String email, String password, HttpSession session){
        AdminUser userByEmpwd = aus.getUserByEmpwd(email, password);
        if (userByEmpwd!=null) {
            session.setAttribute(Const.IS_LOGIN,userByEmpwd);
            return ResponseResult.getSuccess();
        }
        return ResponseResult.getFail("账号密码不正常");
    }
    @RequestMapping("/logout")
    public void logout(HttpSession session, HttpServletResponse response) throws IOException {
        session.invalidate();
        response.sendRedirect("/login.html");
    }

    @RequestMapping("/table")
    public ResultType table(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit, String realName, String email, Integer status){
        return aus.table(page,limit,realName,email,status);
    }

    @RequestMapping("/assign_role")
    public ResponseResult getassign(Integer userId,Integer[] roleIds){
        return aus.getassign(userId,roleIds);
    }
    @RequestMapping("/user_role")
    public List<Integer> getuserRole(Integer userId){
        return aus.getuserRole(userId);
    }
    @RequestMapping("/add")
    public ResponseResult addUser(AdminUser adminUser){
        try {
            return aus.addUser(adminUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("添加失败");
    }
    @RequestMapping("/update")
    public ResponseResult updateUser(AdminUser adminUser){
        try {
            return aus.updateUser(adminUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("修改失败");
    }
    @RequestMapping("/del")
    public ResponseResult delUser(Integer[] ids){
        try {
            return aus.delUser(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.getFail("删除失败");
    }

}
